#!/usr/bin/env bash

requirements_solaris_lib_installed()
{
  pkg info "$1" > /dev/null 2>&1 || return $?
}

requirements_solaris_lib_available()
{
  pkg search "$1" 2>/dev/null | __rvm_grep "^$1-" > /dev/null 2>&1 || return $?
}

requirements_solaris_custom_lib_installed()
{
  pkginfo -q "$1" || return $?
}

requirements_solaris_libs_install()
{
  __rvm_try_sudo pkg install "$@" ||
  {
    \typeset ret=$?
    case $ret in
      (4) return 0 ;; # means the package does not need updates
    esac
    return $ret
  }
}

requirements_solaris_update_system()
{
  case "${_system_version}" in
    (10)
      return 0
      ;;
    (*)
      __rvm_try_sudo pkg refresh ||
      {
        \typeset ret=$?
        case $ret in
          (4) return 0 ;; # means nothing to install
        esac
        return $ret
      }
      ;;
  esac
}

requirements_solaris_check_custom()
{
  for lib in "$@"
  do
    [[ " ${packages_custom[*]} " == *" $lib "* ]] ||
    requirements_solaris_custom_lib_installed "$lib" || __rvm_add_once packages_custom "$lib"
  done
  unset lib
}

requirements_solaris_check_opencsw_enabled()
{
  requirements_solaris_custom_lib_installed CSWpkgutil || return $?
}

requirements_solaris_enable_opencsw()
{
  __rvm_try_sudo pkgadd -a $rvm_path/config/solaris/noask -d https://get.opencsw.org/now CSWpkgutil > /dev/null 2>&1 || return $?
}

requirements_solaris_install_custom()
{
  requirements_solaris_check_opencsw_enabled || requirements_solaris_enable_opencsw
  __rvm_try_sudo /opt/csw/bin/pkgutil -iy "$@" -t https://mirror.opencsw.org/opencsw/unstable || return $?
}

requirements_solaris_define()
{
  if
    is_a_function requirements_solaris_${_system_version}_define
  then
    requirements_solaris_${_system_version}_define "$@" || return $?
  else
    rvm_error "Solaris ${_system_version} is not supported"
    return 40
  fi
}

requirements_solaris_11_update_after()
{
  __rvm_try_sudo pkg update "${packages_installed[@]}" ||
  {
    \typeset ret=$?
    case $ret in
      (4) return 0 ;; # means nothing to install
    esac
    return $ret
  }
}

requirements_solaris_after()
{
  case "${_system_version}" in
    (10)
      return 0
      ;;
    (*)
      if
        [[ -n "${packages_installed[*]}" ]]
      then
        rvm_requiremnts_fail_or_run_action 3 \
          "Updating installed packages: ${packages_installed[*]}" \
          requirements_solaris_11_update_after ||
          return $?
      fi
      ;;
  esac
}

requirements_solaris_10_define()
{
  case "$1" in
    (rvm)
      requirements_solaris_check_custom CSWbash CSWgit CSWgpatch CSWmd5deep CSWwhich
      ;;

    (jruby-head*)
      requirements_solaris_check_custom git make
      ;;

    (jruby*)
      # JDK is already installed on Solaris 10
      # TODO: is ant installed also?
      requirements_solaris_check_custom make

      if
        is_head_or_disable_binary "$1"
      then
        requirements_solaris_check_custom git
        case $( jruby_installation_method  "$1" ) in
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      fi
      ;;

    (ir*)
      # No support
      true
      ;;

    (opal)
      # No support
      true
      ;;

    (*-head)
      requirements_solaris_check_custom CSWgpatch CSWgcc4core CSWgmake CSWcoreutils CSWpkgconfig CSWautoconf CSWautomake CSWlibtool CSWlibyaml-dev CSWlibreadline-dev CSWlibncurses-dev CSWlibssl-dev CSWcurl CSWgit
      __rvm_update_configure_opt_dir "$1" "/opt/csw"
      ;;

    (*)
      requirements_solaris_check_custom CSWgpatch CSWgcc4core CSWgmake CSWcoreutils CSWpkgconfig CSWautoconf CSWautomake CSWlibtool CSWlibyaml-dev CSWlibreadline-dev CSWlibncurses-dev CSWlibssl-dev CSWcurl
      __rvm_update_configure_opt_dir "$1" "/opt/csw"
      ;;
  esac
}

requirements_solaris_11_define()
{
  case "$1" in
    (rvm)
      requirements_check bash curl text/gnu-patch
      requirements_check_fallback git scmgit-base
      ;;
    (jruby*)
      # This will fail with the message
      ## To indicate that you agree to and accept the terms of the licenses of the packages listed above, use the --accept option.
      ## To display all of the related licenses, use the --licenses option.
      # Need to handle this somehow
      if
        is_head_or_disable_binary "$1"
      then
        requirements_check jdk
        requirements_check_fallback git scmgit-base
        case $( jruby_installation_method  "$1" ) in
          ant) requirements_check ant                    ;;
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      else
        requirements_check jdk
      fi
      ;;
    (ir*)
      # No support
      true
      ;;
    (opal)
      # No support
      true
      ;;
    (*-head)
      requirements_check_fallback git scmgit-base
      requirements_check text/gnu-patch developer/gcc-45 system/header developer/build/autoconf developer/build/automake developer/build/libtool system/library/math/header-math file/gnu-coreutils
      requirements_solaris_check_custom CSWlibyaml-dev
      __rvm_update_configure_opt_dir "$1" "/opt/csw"
      ;;
    (*)
      requirements_check text/gnu-patch developer/gcc-45 system/header developer/build/autoconf developer/build/automake developer/build/libtool system/library/math file/gnu-coreutils
      requirements_solaris_check_custom CSWlibyaml-dev
      __rvm_update_configure_opt_dir "$1" "/opt/csw"
      ;;
  esac
}
